import openpyxl
from common import Path_API
import os


def read_excel(sheet1):
    """

    :param sheet1: 接收表名
    :return:
    """
    # 获取Excel表数据
    # work = openpyxl.load_workbook(r"C:\Users\Ju-Ta\Desktop\python自动化\QCD_interface\data\interface_data.xlsx")
    work = openpyxl.load_workbook(os.path.join(Path_API.DATA_DIR, "interface_data.xlsx"))
    # 获取表单名
    sheet = work[sheet1]
    # 整行获取数据
    res = list(sheet.rows)
    # print(res)
    # Excel数据封装成字典
    title = [i.value for i in res[0]]
    # print(title)
    list1 = []
    for i in res[1:]:
        list2 = [j.value for j in i]
        list1.append(dict(zip(title, list2)))
    # print(list1)
    return list1


def write_execl(sheet1, row, column, value):
    work = openpyxl.load_workbook(os.path.join(Path_API.DATA_DIR, "interface_data.xlsx"))
    sheet = work[sheet1]
    # 填入数据
    sheet.cell(row=row, column=column, value=value)
    work.save(os.path.join(Path_API.DATA_DIR, "interface_data.xlsx"))
